<?php
namespace App\Http\Controllers\Api\Trade;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\Controller;
use App\Http\Requests\Trade\Demand\ListRequest;
use App\Http\Resources\Trade\Demand\ListResource as DemandListResource;
use App\Http\Resources\Trade\Demand\DetailResource as DemandDetailResource;
use App\Http\Resources\Trade\Shop\DetailResource as ShopDetailResource;
use App\Http\Resources\Base\UserResource;
use App\Models\Trade\Demand\DemandModel;
use App\Models\Trade\Demand\DemandReportModel;
use App\Models\Trade\Demand\DemandCollectionModel;
use App\Services\Trade\DemandService;

class DemandController extends Controller
{

    /**
     * 求购列表
     *
     * @param $page 可选            
     * @param $page_size 可选            
     * @param $verify 可选            
     * @param $order 可选            
     * @param $shop_id 可选            
     * @param $kind_id 可选            
     * @param $province 可选            
     * @param $city 可选            
     * @param $keyword 可选            
     */
    public function getList(ListRequest $request)
    {
        $page = intval($request->query('page', 1));
        $pageSize = intval($request->query('page_size', 50));
        $order = $request->query('order', DemandModel::ORDER_DEFAULT);
        $param = $request->all();
        
        $demandService = new DemandService();
        list ($collection, $count) = $demandService->selectList($page, $pageSize, $param, $order);
        
        $data['count'] = [
            'total' => $count,
            'page' => $page,
            'page_size' => $pageSize
        ];
        $data['data'] = new DemandListResource($collection);
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 求购详情
     *
     * @param $id 必选            
     */
    public function getDetail(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->query('id');
        
        $demandService = new DemandService();
        $data = $demandService->findDetail($id)->shift();
        $data = DemandDetailResource::makeDataDecode($data);
        $data = UserResource::makeSignDecode($data);

        // 举报判断
        $data['report'] = $this->identity()->isValid() && DemandReportModel::findItem($this->identity()->getUserId(), $id)->count() ? 1 : 0;
        // 收藏判断
        $data['collection'] = $this->identity()->isValid() && DemandCollectionModel::findItem($this->identity()->getUserId(), $id)->count() ? 1 : 0;
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 求购详情页辅助信息
     *
     * @param $id 必选            
     * @param $page_size 可选            
     */
    public function getDetailSub(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1',
            'page_size' => 'nullable|integer|between:1,100'
        ]);
        $id = $request->query('id');
        $pageSize = $request->query('page_size', 3);
        
        $demandService = new DemandService();
        // 店铺信息
        $data['shop'] = ShopDetailResource::makeDataDecode($demandService->findDetailShop($id)->shift());
        // 推荐列表
        list ($collection, $count) = $demandService->selectList(1, $pageSize, [
            'shop_id' => $data['shop']['id'],
            'id' => $id
        ]);
        $data['dataList'] = new DemandListResource($collection);
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 求购详情页推荐列表
     * 
     * @param $id 必选            
     * @param $page_size 可选   
     */
    public function getDetailMoers(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1',
            'page_size' => 'nullable|integer|between:1,100'
        ]);
        $id = $request->query('id');
        $pageSize = $request->query('page_size', 5);

        $demandService = new DemandService();
        list ($collection, $count) = $demandService->selectDatailMoers($id, $pageSize);
        
        $data = new DemandListResource($collection);
        
        return $this->response->setResponseData($data)->getResponse();
    }
}
